package top.zhangjianyong.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class PhotoUtil {

    public static void main(String[] args) throws Exception {
        /*File fileA = new File("/Users/zhangjianyong/Documents/测试/output/1656036593935.jpg");
        File fileB = new File("/Users/zhangjianyong/Documents/测试/output/1656036593959.jpg");
        byte[] bytesA = IOUtils.toByteArray(new FileInputStream(fileA));
        byte[] bytesB = IOUtils.toByteArray(new FileInputStream(fileB));
        float percent = compare(bytesA, bytesB);
        if (percent == 0) {
            System.out.println("无法比较");
        } else {
            System.out.println("两张图片的相似度为：" + percent + "%");
        }*/


        File file = new File("/Users/zhangjianyong/Downloads/437d33f2edffff5cab3c8d9c8036d766.jpeg");
        List<BufferedImage> bufferedImages = cutImage(ImageIO.read(file),2,2);

        bufferedImages.stream().forEach(bufferedImage -> {
            try {
                ImageIO.write(bufferedImage,"jpg",
                        new File("/Users/zhangjianyong/Downloads/"+
                                UUID.randomUUID()+".jpg"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }


    public static int[] getData(InputStream inputStream) {
        try {
            BufferedImage img = ImageIO.read(inputStream);
            return getData(img);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("有文件没有找到,请检查文件是否存在或路径是否正确");
            return null;
        }

    }

    public static int[] getData(String name) {
        try {
            BufferedImage img = ImageIO.read(new File(name));
            return getData(img);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("有文件没有找到,请检查文件是否存在或路径是否正确");
            return null;
        }
    }

    public static int[] getData(BufferedImage img) {

        BufferedImage slt = new BufferedImage(100, 100,
                BufferedImage.TYPE_INT_RGB);
        slt.getGraphics().drawImage(img, 0, 0, 100, 100, null);
        // ImageIO.write(slt,"jpeg",new File("slt.jpg"));
        int[] data = new int[256];
        for (int x = 0; x < slt.getWidth(); x++) {
            for (int y = 0; y < slt.getHeight(); y++) {
                int rgb = slt.getRGB(x, y);
                Color myColor = new Color(rgb);
                int r = myColor.getRed();
                int g = myColor.getGreen();
                int b = myColor.getBlue();
                data[(r + g + b) / 3]++;
            }
        }
        // data 就是所谓图形学当中的直方图的概念
        return data;

    }

    public static float compare(byte[] bytesA, byte[] bytesB) {
        ByteArrayInputStream byteArrayInputStreamA = new ByteArrayInputStream(bytesA);
        ByteArrayInputStream byteArrayInputStreamB = new ByteArrayInputStream(bytesB);
        int[] dataA = getData(byteArrayInputStreamA);
        int[] dataB = getData(byteArrayInputStreamB);
        return compare(dataA, dataB);
    }

    public static float compare(int[] s, int[] t) {
        try {
            float result = 0F;
            for (int i = 0; i < 256; i++) {
                int abs = Math.abs(s[i] - t[i]);
                int max = Math.max(s[i], t[i]);
                result += (1 - ((float) abs / (max == 0 ? 1 : max)));
            }
            return (result / 256) * 100;
        } catch (Exception exception) {
            return 0;
        }
    }

    public static List<BufferedImage> cutImage(BufferedImage img,int cols,int rows) {
        List<BufferedImage> list = new ArrayList<>();
        int lw = img.getWidth() / rows;
        int lh = img.getHeight() / cols;
        for (int i = 0; i < rows; i++) {
            int x = lw * i;
            for (int j = 0; j < cols; j++) {
                int y = lh * j;
                BufferedImage bufferedImage = img.getSubimage(x, y, lw, lh);
                //System.out.println(x+","+y);
                list.add(bufferedImage);
            }
        }
        return list;
    }


}